package com.example.learn_spring_boot3.controller;

import com.example.learn_spring_boot3.service.FileService;
import com.example.learn_spring_boot3.utils.ResponseData;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public ResponseEntity<ResponseData<Object>> upload(@ModelAttribute("file") MultipartFile file, @ModelAttribute("dir") String dir) throws IOException {
        return fileService.upload(file, dir);
    }

    @GetMapping("/download/{dir}/{fileName}")
    public void download(@PathVariable String dir, @PathVariable String fileName, HttpServletResponse response) {
        fileService.download(dir, fileName, response);
    }
}
